package com.xudow.app.ui.task.qanda;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

import com.activeshare.edu.ucenter.common.messages.qanda.QuestionMessage;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.util.StringUtils;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 在1对1提问时，用户自己提交内容，作为自身问题的回答 Task
 *
 * @author lukecheng
 */
public class QuestionSubmitSelfAnswerTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(QuestionSubmitSelfAnswerTask.class);

    private static final String QANDA_QUESTION_SUBMIT_SELF_ANSWER_URL = Config.BASE_URL + Config.QANDA_QUESTION_SUBMIT_SELF_ANSWER_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;
    public static final int ERROR_WITH_MESSAGE = 2;

    private Context context;
    private Handler handler;

    public QuestionSubmitSelfAnswerTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(QuestionMessage result) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        if (result.getData() != null) {
            Bundle bundle = new Bundle();
            bundle.putLong("questionId", Long.parseLong(result.getData().toString()));
            message.setData(bundle);
        }

        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
        XApplication application = (XApplication) context.getApplicationContext();

        MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();
        valueMap.add("answer", parameters[0].get("answer"));

        if (!StringUtils.isEmpty(parameters[0].get("question_id"))) {
            valueMap.add("question_id", parameters[0].get("question_id"));
        }

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        try {
            QuestionMessage result = restTemplate.postForObject(QANDA_QUESTION_SUBMIT_SELF_ANSWER_URL,
                    valueMap, QuestionMessage.class);

            if (result != null) {
                if (result.getResult().equals("0")) {
                    sendSuccessMessage(result);
                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        } catch (Exception e) {
            LOGE(TAG, e.getMessage());

            sendErrorMessage(null);
        }

        return null;
    }
}
